﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;

public class GameData//游戏存档
{
    public DateTime saveTime;
    public int gameDataID;
    public UserData userData;//玩家信息
    public StageData stageData;//场景信息
    public PlayerData playerData;
    public SettingData settingData;

    //更多数据类可以在这里扩展
    public GameData(int gameDataID)//添加全局存档时调用
    {
        #region 初始化玩家数据
        userData = new UserData();
        #endregion
        stageData = new StageData();

        saveTime = DateTime.Now;
        this.gameDataID = gameDataID;
    }
    
    #region 用于Json反序列化
    [JsonConstructor]
    public GameData(int gameDataID,UserData userData,SettingData settingData)
    {
        this.userData = userData;
        this.gameDataID = gameDataID;
        this.settingData = settingData;
    }
    #endregion

    public void UpdateSaveTime()
    {
        saveTime = DateTime.Now;
    }
}

public class DataService :DataServiceBase, ILogic
{
    public void OnInit()
    {
        ReadInitData();
        UpdateCurrentGameDataID(-1);//表示没有进入任何存档点
        if (!GameRoot.Instance.EnableGameSave)
        {
            return;
        }
        if (GameDataDic.Count == 0)
        {
            CreateNewGameData();
        }
    }
    public void OnUpdate()
    {
        
    }
    public void UnInit()
    {
      CacheAndSaveLastGameData();
    }
    public void OnEnterState()
    {
       
    }
    private void UpdateMonoItem(StageData stageData)
    {
        foreach (var item in GameRoot.Instance.monoItemSystem.MonoItemDic)
        {
            if (!item.Value.isSaveData)
            {
                continue;
            }
            MonoItemInfo monoItemInfo = new MonoItemInfo()
            {
                Pos = new SerializeVector3(item.Value.transform.position.x, item.Value.transform.position.y, item.Value.transform.position.z),
                EulerAngle = new SerializeVector3(item.Value.transform.eulerAngles.x, item.Value.transform.eulerAngles.y, item.Value.transform.eulerAngles.z),
                isUnActive = !item.Value.gameObject.activeSelf,
            };
            //暂时不做存档性能优化
            stageData.MonoItemDic[item.Key] = monoItemInfo;
        }
    }
    public override void CacheGameData(GameData gameData)
    {

    }

    public SettingData GetLastDataSettingData()
    {
        if (GetLastGameData()?.settingData == null)
        {
            GetLastGameData().settingData = new SettingData();
        }
        return GetLastGameData().settingData;
    }

}
//存档的存：游戏中存、游戏退出存：都可以在这里调用模块接口获取对象数据来保存